package com.example.cathouse;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;

import com.bumptech.glide.Glide;
import com.example.cathouse.databinding.CatDetailFragmentBinding;
import com.google.android.material.bottomnavigation.BottomNavigationView;

public class Cat_Detail_Fragment extends Fragment {
    CatDetailFragmentBinding binding;
    private CatViewModel catViewModel;
    private LiveData<Cat> cat;

    public static Cat_Detail_Fragment newInstance() {
        return new Cat_Detail_Fragment();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        catViewModel = new ViewModelProvider(requireActivity()).get(CatViewModel.class);
        BottomNavigationView bottomNavigationView = getActivity().findViewById(R.id.bottomNavigationView);
        bottomNavigationView.setVisibility(View.GONE);      //隐藏底部导航栏

        binding = DataBindingUtil.inflate(inflater, R.layout.cat__detail__fragment, container, false);
        binding.setData(catViewModel);
        binding.setLifecycleOwner(requireActivity());
        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        cat = catViewModel.getCat(getArguments().getInt("cat_id"));
        cat.removeObservers(requireActivity());
        cat.observe(requireActivity(), new Observer<Cat>() {
            @Override
            public void onChanged(Cat cat) {
                if (cat != null) {
                    binding.textViewAppearance.setText(cat.getCatAppearance());
                    binding.textViewCondition.setText(cat.getCatCondition());
                    binding.textViewSex.setText(cat.getCatSex());
                    binding.textViewSterilization.setText(cat.getCatSterilization());
                    binding.textViewName.setText(cat.getCatName());
                    binding.textViewCharacter.setText(cat.getCatCharacter());
                    binding.textViewColor.setText(cat.getCatColor());
                    binding.textViewRelationship.setText(cat.getCatRelationship());

                    String url = "https://636c-cloud1-5g779hyxe755f01d-1305146776.tcb.qcloud.la/";
                    if (isAdded()) {
                        Glide.with(requireActivity())
                                .load(url + cat.getCatName() + ".jpg")
                                .placeholder(R.drawable.ic_loading)
                                .error(R.drawable.ic_error)
                                .into(binding.imageViewCat);
                    }
                }
                binding.floatingActionButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Bundle bundle = new Bundle();
                        bundle.putInt("cat_id", cat.getId());
                        NavController controller = Navigation.findNavController(v);
                        controller.navigate(R.id.action_cat_Detail_Fragment_to_modifyFragment, bundle);
                    }
                });
            }

        });

    }

    @Override
    public void onResume() {
        InputMethodManager imm = (InputMethodManager) requireActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(requireView().getWindowToken(), 0);  // 提交后隐藏键盘
        super.onResume();
    }
}